//
//  TrainSMSVerificationView.swift
//  AirTravel
//
//  Created by apple on 2021/8/19.
//

import UIKit
import FWPopupView

protocol TrainSMSVerificationDelegate:NSObjectProtocol {
    func trainSMSVerificationClick(input:String)
}

class TrainSMSVerificationView: FWPopupView {
    var contentView:UIView!
    weak var delegate: TrainSMSVerificationDelegate?
    
    @IBOutlet weak var inputTf: UITextField!
    @IBOutlet weak var determineBtn: UIButton!
    @IBOutlet weak var cancelBtn: UIButton!
    
    // 初始化时将xib中的view添加进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadViewFromNib()
        contentView.frame = self.bounds
        contentView.backgroundColor = UIColor.white
        self.insertSubview(contentView, at: 0)
        
        self.cancelBtn.layer.cornerRadius = 6
        self.cancelBtn.layer.borderWidth = 1
        self.cancelBtn.layer.borderColor = UIColor.groupTableViewBackground.cgColor
        
        self.determineBtn.layer.cornerRadius = 6
        self.determineBtn.layer.masksToBounds = true
    }
    
    // 初始化时将xib中的view添加进来
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // 加载xib
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        view.backgroundColor = UIColor.clear
        return view
    }
    
    
    @IBAction func cancel(_ sender: Any) {
        self.hide()
        self.hideKeyboard()
    }
    
    
    @IBAction func determine(_ sender: Any) {
        if self.inputTf.text?.count != 0 {
            self.delegate?.trainSMSVerificationClick(input: self.inputTf.text ?? "")
            self.hide()
            self.hideKeyboard()
        }else{
            YProgressHUD.showToast("核验码不能为空")
        }
    }
}
